package homePage.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import homePage.bean.Book;
import homePage.dao.BookSearchDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class BookSearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        InputStreamReader inputStreamReader = new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder str = new StringBuilder();
        int respInt = inputStreamReader.read();
        while (respInt!=-1){
            str.append((char) respInt);
            respInt = inputStreamReader.read();
        }
        String jsonString =String.valueOf(str);
        JSONObject jsonObject= JSONObject.parseObject(jsonString);


        String type = jsonObject.getString("searchType");

        BookSearchDao bookDao = new BookSearchDao();
        System.out.println(type);

        switch (type){
            case "bookNameSearch":
                String bookName = jsonObject.getString("searchValue");

                List<Book> nameList = new ArrayList<>();

                nameList = bookDao.getByName(bookName);
                if(nameList.isEmpty()){
                    out.print("{}");

                }else {
                    out.print(JSON.toJSONString(nameList));
                }
                break;
            case "authorSearch":
                String author = jsonObject.getString("searchValue");
                List<Book> authorList = bookDao.getByAuthor(author);
                if(authorList.isEmpty()){
                    out.print("{}");
                }else {
                    out.print(JSON.toJSONString(authorList));
                }
                break;
            case "kindSearch":
                String kind = jsonObject.getString("searchValue");
                List<Book> kindList = bookDao.getByKind(kind);
                if(kindList.isEmpty()){
                    out.print("{}");
                }else {
                    out.print(JSON.toJSONString(kindList));
                }
                break;
            case "allBook":
                List<Book> allBookList= bookDao.getAllBook();
                if(allBookList.isEmpty()){
                    out.print("{}");
                }else {
                    out.print(JSON.toJSONString(allBookList));
                }
                break;
        }


    }

}
